const { data } = require("jquery");
const mongoose = require("mongoose");

// 查询影片
const movies = async () => {
    const moviesModel = mongoose.model("movies");
    // 获取数据总数
    const total = await moviesModel.countDocuments();
    // 获取总数据
    const data = await moviesModel.find().skip(0).limit(total).exec();
    return data
};
// 查询影院
const cinema = async () => {
    const cinemasModel = mongoose.model("cinemas");
    const total = await cinemasModel.countDocuments();
    const data = await cinemasModel.find().skip(0).limit(total).exec();;
    return data
};
// 查询放映厅
const projection = async () => {
    const projectionHallModel = mongoose.model("projectionHall");
    const total = await projectionHallModel.countDocuments();
    // 获取所有放映厅
    const data = await projectionHallModel.find().skip(0).limit(total).exec();;
    return data
};
// 新增排片信息
const save = async ({ movieId, cinemasId, theaterId, showTime, price }) => {
    const schedulesModel = mongoose.model("schedules");
    // 1、新增排片信息
    const { _id: scheduleId } = await schedulesModel.create({ movieId, cinemasId, theaterId, showTime, price });
    // 2、获取排片信息中，涉及的放映厅所对应的座位信息
    const seatsArr = await mongoose.model("seats").find({ projectionHallId: theaterId }).sort({ _id: -1})
    // 创建属于当前排片信息的座位数据
    const seatingsArr = seatsArr.map(item => ({
        seatId: item._id,
        scheduleId,
        status: 0
    }));
    // 把座位信息加入数据库
    const data = await mongoose.model("seatings").create(seatingsArr);
    // const total = await schedulesModel.countDocuments();
    // // // 获取全部数据
    // const data = await schedulesModel.find().populate("cinemasId").populate("movieId").populate("theaterId").skip(0).limit(total).sort({ _id: -1 }).exec();
    return data
};
// 获取排片数据
const schedules = async () => {
    const schedulesModel = mongoose.model("schedules");
    const total = await schedulesModel.countDocuments();
    // 获取全部数据并填充数据
    const data = await schedulesModel.find().populate("cinemasId").populate("movieId").populate("theaterId").skip(0).limit(total).sort({ _id: -1 }).exec();
    return data
};
// 获取座位数据
const seatings = async (scheduleId) => {
    // 获取座位数据
    const seatsingArr = await mongoose.model("seatings").find({ scheduleId }).populate("seatId").populate("scheduleId").sort({ _id: 0 });
    return seatsingArr;
};
// 新增订单信息
const indent = async (data) => {
    const seatingsModel = mongoose.model("seatings")
    // 遍历添加数据
    for (let i = 0; i < data.length; i++) {
        await seatingsModel.updateOne({ _id: data[i].seatingsId }, { status: 1 });
    };
    data.map((item) => {
        mongoose.model("indent").create(item);
    })
};

// 查询订单信息
const gain = async () => {
    const schedulesModel = mongoose.model("schedules");
    const seatsModel = mongoose.model("seatings");
    const indentModel = mongoose.model("indent");
    const total = await indentModel.countDocuments();
    // 返回填充订单信息
    let data = await indentModel.find().populate("admisId").skip(0).limit(total).sort({ _id: -1 }).exec();
    let seatsArr = [];
    // 循环获取需要的数据
    console.log(data);
    for (let i = 0; i < data.length; i++) {
        let schedule = await schedulesModel.find({ _id: data[i].scheduleId }).populate("cinemasId").populate("movieId").populate("theaterId");
        let seatings = await seatsModel.find({ _id: data[i].seatingsId }).populate("seatId").populate("scheduleId").sort({ _id: 0 });
        // 把需要的数据填集合在新的数组
        seatsArr.push({
            name: data[i].admisId.adminName,
            seatingsId: seatings,
            scheduleId: schedule,
        })
    }
    return seatsArr;
};

// 查询电影对应排片信息
const getByMovie = async ({ movieId }) => {
    const schedulesModel = mongoose.model("schedules");
    const data = await schedulesModel.find({ movieId });
    return data;
}

// 查询一条对应的排片数据
const getOne = async ({ _id }) => {
    const schedulesModel = mongoose.model("schedules");
    const data = await schedulesModel.findOne({ _id });
    const cinemasModel = mongoose.model("cinemas");
    const cinema = await cinemasModel.findOne({ _id: data.cinemasId });
    const moviesModel = mongoose.model("movies");
    const movie = await moviesModel.findOne({ _id: data.movieId });
    const projectionHallModel = mongoose.model("projectionHall");
    const theater = await projectionHallModel.findOne({ _id: data.theaterId });
    const result = { schedule: data, cinema, movie, theater }
    return result;
}

// 暴露方法
module.exports = { movies, cinema, projection, save, schedules, seatings, indent, gain, getByMovie, getOne };